package com.ruoyi.xiaoshou.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.system.api.domain.store.StockIn;
import com.ruoyi.xiaoshou.domain.SaleOrder;
import com.ruoyi.xiaoshou.dto.SaleBackDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.domain.store.StockIn;
import com.ruoyi.xiaoshou.domain.SaleBack;
import com.ruoyi.xiaoshou.dto.SaleBackDto;
import com.ruoyi.xiaoshou.service.ISaleBackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 销售退货单Controller
 *
 * @author lmj
 * @date 2024-10-29
 */
@RestController
@RequestMapping("/saleBack")
@Slf4j
public class SaleBackController extends BaseController {
    @Autowired
    private ISaleBackService saleBackService;

    /**
     * 查询销售退货单列表
     */
    @RequiresPermissions("xiaoshou:saleBack:list")
    @GetMapping("/list")
    public TableDataInfo list(SaleBack saleBack) {
        startPage();
        List<SaleBack> list = saleBackService.selectSaleBackList(saleBack);
        return getDataTable(list);
    }


    @RequiresPermissions("xiaoshou:saleBack:list")
    @GetMapping("/noReconList")
    public TableDataInfo noReconList(SaleBack saleBack) {
        startPage();
        List<SaleBack> list = saleBackService.noReconList(saleBack);
        return getDataTable(list);
    }

    @GetMapping("/listForReciveOrder")
    public TableDataInfo listForReciveOrder(SaleBack saleBack) {
        startPage();
        List<SaleBack> list = saleBackService.selectSaleBackListForReciveOrder(saleBack);
        logger.info("listForReciveOrder list:{}", list);
        return getDataTable(list);
    }

    @GetMapping("/getSaleBackListByOrderCodes/{codes}")
    public AjaxResult getSaleBackListByOrderCodes(@PathVariable("codes") String[] codes) {
        logger.info("SaleOrderController getSaleBackListByOrderCodes params:{}", Arrays.toString(codes));
        List<SaleBack> saleOrderList = saleBackService.getSaleBackListByOrderCodes(codes);
        return AjaxResult.success(saleOrderList);
    }

    @RequiresPermissions("xiaoshou:saleBack:list")
    @GetMapping("/getUseList")
    public TableDataInfo getUseList(SaleBack saleBack) {
        startPage();
        List<SaleBack> list = saleBackService.getUseList(saleBack);
        return getDataTable(list);
    }

    /**
     * 导出销售退货单列表
     */
    @RequiresPermissions("xiaoshou:saleBack:export")
    @Log(title = "销售退货单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SaleBack saleBack) {
        List<SaleBack> list = saleBackService.selectSaleBackList(saleBack);
        ExcelUtil<SaleBack> util = new ExcelUtil<SaleBack>(SaleBack.class);
        util.exportExcel(response, list, "销售退货单数据");
    }

    /**
     * 获取销售退货单详细信息
     */
    @RequiresPermissions("xiaoshou:saleBack:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(saleBackService.selectSaleBackById(id));
    }
    /**
     * 获取销售订单今日笔数及金额
     */
    @RequiresPermissions("xiaoshou:saleBack:query")
    @GetMapping(value = "getSaleBackCountAndMoney")
    public AjaxResult getSaleBackCountAndMoney() {
        return success(saleBackService.getSaleBackCountAndMoney());
    }
    @PostMapping("/getSaleBackByCode")
    public R<SaleBack> getSaleBackByCode(@RequestBody String code) {
        log.info("SaleBackController getSaleBackByCode params:{}", code);
        String[] codes = new String[]{code};
        List<SaleBack> saleBacks = saleBackService.getSaleBackListByOrderCodes(codes);
        if (saleBacks.size() == 1) {
            return R.ok(saleBacks.get(0));
        } else {
            return R.fail();
        }
    }

    @InnerAuth
    @PostMapping("/innerEditByCode")
    public R<Boolean> innerEditByCode(@RequestBody SaleBack saleBack) {
        return R.ok(saleBackService.updateSaleBackyCode(saleBack) > 0);
    }

    /**
     * 新增销售退货单
     */
    @RequiresPermissions("xiaoshou:saleBack:add")
    @Log(title = "销售退货单", businessType = BusinessType.INSERT)
    @PostMapping("addSaleBack")
    public AjaxResult addSaleBack(@RequestBody SaleBackDto saleBackDto) {
        log.info("SaleBackController addSaleBack params:{}", saleBackDto);
        return saleBackService.insertSaleBack(saleBackDto);
    }

    @PostMapping("refreshStatusByStockIn")
    public R<String> refreshStatusByStockIn(@RequestBody StockIn stockIn) {
        log.info("SaleBackController refreshStatusByStockIn params:{}", stockIn);
        return R.ok(saleBackService.refreshStatusByStockIn(stockIn));
    }

    /**
     * 修改销售退货单
     */
    @RequiresPermissions("xiaoshou:saleBack:edit")
    @Log(title = "销售退货单", businessType = BusinessType.UPDATE)
    @PostMapping("updSaleBack")
    public AjaxResult updSaleBack(@RequestBody SaleBackDto saleBackDto) {
        log.info("SaleBackController updSaleBack params:{}", saleBackDto);
        return saleBackService.updateSaleBack(saleBackDto);
    }

    @RequiresPermissions("xiaoshou:saleBack:edit")
    @Log(title = "销售退货单", businessType = BusinessType.UPDATE)
    @PostMapping("checkSaleBack")
    public AjaxResult checkSaleBack(@RequestBody SaleBack saleBack) {
        log.info("SaleBackController checkSaleBack params:{}", saleBack);
        return saleBackService.checkSaleBack(saleBack);
    }

    /**
     * 删除销售退货单
     */
    @RequiresPermissions("xiaoshou:saleBack:remove")
    @Log(title = "销售退货单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        log.info("SaleBackController remove params:{}", ids);
        return saleBackService.deleteSaleBackByIds(ids);
    }
}
